package servlet;

import dao.SupplierDAOImpl;
import pojo.Supplier;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/supplier")
public class SupplierServlet extends BaseServlet {

    // 添加供应商
    protected void addSupplier(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        int supplierID = Integer.parseInt(req.getParameter("supplierID"));
        String supplierName = req.getParameter("supplierName");
        String supplierAddress = req.getParameter("supplierAddress");

        Supplier supplier = new Supplier(null, supplierName, supplierAddress);
        SupplierDAOImpl.add(supplier);
        sendJsonResponse(resp, new ApiResult(200, "Supplier added successfully.", null));
    }

    // 删除供应商
    protected void deleteSupplier(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int supplierID = Integer.parseInt(req.getParameter("supplierID"));
        SupplierDAOImpl.delete(supplierID);
        sendJsonResponse(resp, new ApiResult(200, "Supplier deleted successfully.", null));
    }

    // 更新供应商
    protected void updateSupplier(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int supplierID = Integer.parseInt(req.getParameter("supplierID"));
        String supplierName = req.getParameter("supplierName");
        String supplierAddress = req.getParameter("supplierAddress");

        Supplier supplier = new Supplier(supplierID, supplierName, supplierAddress);
        SupplierDAOImpl.update(supplier);
        sendJsonResponse(resp, new ApiResult(200, "Supplier updated successfully.", null));
    }

    // 获取所有供应商
    protected void getAllSuppliers(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Supplier> suppliers = SupplierDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", suppliers));
    }

    // 根据ID获取供应商
    protected void getSupplierById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int supplierID = Integer.parseInt(req.getParameter("supplierID"));
        Supplier supplier = SupplierDAOImpl.getById(supplierID);
        sendJsonResponse(resp, new ApiResult(200, "Success", supplier));
    }

    // 获取供应商数量
    protected void getSupplierCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = SupplierDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取供应商
    protected void getSuppliersByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Supplier> suppliers = SupplierDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", suppliers));
    }

    // 根据关键字搜索供应商
    protected void findSuppliersByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Supplier> suppliers = SupplierDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", suppliers));
    }

    // 根据关键字分页搜索供应商
    protected void findSuppliersByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Supplier> suppliers = SupplierDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", suppliers));
    }
}
